package com.lxmes.base.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lxmes.base.domain.JcWarehouseinfo;
import com.lxmes.base.service.IJcWarehouseinfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.enums.BusinessType;
import com.lxmes.base.domain.JcWarehouseinfoKuwei;
import com.lxmes.base.service.IJcWarehouseinfoKuweiService;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.common.core.page.TableDataInfo;

/**
 * 库位管理Controller
 * 
 * @author 张志煜
 * @date 2022-11-17
 */
@Api(tags="仓库管理")
@RestController
@RequestMapping("/base/warehouseinfoKuwei")
public class JcWarehouseinfoKuweiController extends BaseController
{
    @Autowired
    private IJcWarehouseinfoKuweiService jcWarehouseinfoKuweiService;
    @Autowired
    private IJcWarehouseinfoService jcWarehouseinfoService;

    /**
     * 查询库位管理列表
     */
    @PreAuthorize("@ss.hasPermi('base:warehouseinfoKuwei:list')")
    @GetMapping("/list")
    public TableDataInfo list(JcWarehouseinfoKuwei jcWarehouseinfoKuwei)
    {
        startPage();
        List<JcWarehouseinfoKuwei> list = jcWarehouseinfoKuweiService.selectJcWarehouseinfoKuweiList(jcWarehouseinfoKuwei);
        return getDataTable(list);
    }
    @GetMapping("/listAll")
    public AjaxResult listAll()
    {
        JcWarehouseinfoKuwei kuwei=new JcWarehouseinfoKuwei();
        List<JcWarehouseinfoKuwei> list = jcWarehouseinfoKuweiService.selectJcWarehouseinfoKuweiList(kuwei);
        return AjaxResult.success(list);
    }
    /**
     * 导出库位管理列表
     */
    @PreAuthorize("@ss.hasPermi('base:warehouseinfoKuwei:export')")
    @Log(title = "库位管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JcWarehouseinfoKuwei jcWarehouseinfoKuwei)
    {
        List<JcWarehouseinfoKuwei> list = jcWarehouseinfoKuweiService.selectJcWarehouseinfoKuweiList(jcWarehouseinfoKuwei);
        ExcelUtil<JcWarehouseinfoKuwei> util = new ExcelUtil<JcWarehouseinfoKuwei>(JcWarehouseinfoKuwei.class);
        util.exportExcel(response, list, "库位管理数据");
    }
    /*仓库树*/
    @GetMapping("/getWarehouseTree")
    public AjaxResult getWarehouseTree()
    {
        //获取全部仓库
        List<JcWarehouseinfo> warehouseinfoList = jcWarehouseinfoService.selectJcWarehouseinfoList1(new JcWarehouseinfo());
        JSONArray returnArry=new JSONArray();
        for (JcWarehouseinfo warehouseinfo:warehouseinfoList){
            JSONObject ckObj=new JSONObject();
            ckObj.put("value",warehouseinfo.getStockCode());
            ckObj.put("label",warehouseinfo.getStockName());
            //获取所有库区
            JcWarehouseinfoKuwei kq = new JcWarehouseinfoKuwei();
            kq.setStockCode(warehouseinfo.getStockCode());
            List<JcWarehouseinfoKuwei> kqList = jcWarehouseinfoKuweiService.selectJcWarehouseinfoKuQuList(kq);
            JSONArray kqArry=new JSONArray();
            for(JcWarehouseinfoKuwei kqO:kqList){
                JSONObject kqObj=new JSONObject();
                kqObj.put("value",kqO.getKuquCode());
                kqObj.put("label",kqO.getKuquName());
                kqArry.add(kqObj);
                //获取所有库位
                JcWarehouseinfoKuwei kw = new JcWarehouseinfoKuwei();
                kw.setKuquCode(kqO.getKuquCode());
                List<JcWarehouseinfoKuwei> kwList = jcWarehouseinfoKuweiService.selectJcWarehouseinfoKuweiList(kw);
                JSONArray kwArry=new JSONArray();
                for (JcWarehouseinfoKuwei kwO:kwList){
                    JSONObject kwObj=new JSONObject();
                    kwObj.put("value",kwO.getKuweiJmCode());
                    kwObj.put("label",kwO.getKuweiJmCode());
                    kwArry.add(kwObj);
                }
                kqObj.put("children",kwArry);
            }
            ckObj.put("children",kqArry);
            returnArry.add(ckObj);
        }
        System.out.println(returnArry.toJSONString());
        return AjaxResult.success(returnArry);
    }
    @GetMapping("/getListWarehouseinfoKuQu/{stockCode}")
    public AjaxResult getListWarehouseinfoKuQu(@PathVariable("stockCode") String stockCode)
    {
        //获取所有库区
        JcWarehouseinfoKuwei kq = new JcWarehouseinfoKuwei();
        kq.setStockCode(stockCode);
        List<JcWarehouseinfoKuwei> kqList = jcWarehouseinfoKuweiService.selectJcWarehouseinfoKuQuList(kq);
        JSONArray jsonArray=new JSONArray();
        for (int i = 0; i <kqList.size() ; i++) {
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("value",kqList.get(i).getKuquCode());
            jsonObject.put("label",kqList.get(i).getKuquName());
            jsonArray.add(jsonObject);
        }
        return AjaxResult.success(jsonArray);
    }
    @GetMapping("/getListWarehouseinfoKuWei/{kuquCode}")
    public AjaxResult getListWarehouseinfoKuWei(@PathVariable("kuquCode") String kuquCode)
    {
        //获取所有库位
        JcWarehouseinfoKuwei kw = new JcWarehouseinfoKuwei();
        kw.setKuquCode(kuquCode);
        List<JcWarehouseinfoKuwei> kwList = jcWarehouseinfoKuweiService.selectJcWarehouseinfoKuweiList(kw);
        JSONArray jsonArray=new JSONArray();
        for (int i = 0; i <kwList.size() ; i++) {
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("value",kwList.get(i).getKuweiJmCode());
            jsonObject.put("label",kwList.get(i).getKuweiJmCode());
            jsonArray.add(jsonObject);
        }
        return AjaxResult.success(jsonArray);
    }

    /**
     * 获取库位管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:warehouseinfoKuwei:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(jcWarehouseinfoKuweiService.selectJcWarehouseinfoKuweiById(id));
    }
    /*手机端根据库位简码获取库位*/
    @GetMapping(value = "/getWarehouseinfoKuwei/{kuweiJmCode}")
    public AjaxResult getWarehouseinfoKuwei(@PathVariable("kuweiJmCode")String kuweiJmCode){
        return AjaxResult.success( jcWarehouseinfoKuweiService.selectJcWarehouseinfoKuweiByKuweiJmCode(kuweiJmCode));
    }
    /**
     * 新增库位管理
     */
    @PreAuthorize("@ss.hasPermi('base:warehouseinfoKuwei:add')")
    @Log(title = "库位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JcWarehouseinfoKuwei jcWarehouseinfoKuwei)
    {
        return toAjax(jcWarehouseinfoKuweiService.insertJcWarehouseinfoKuwei(jcWarehouseinfoKuwei));
    }

    /**
     * 修改库位管理
     */
    @PreAuthorize("@ss.hasPermi('base:warehouseinfoKuwei:edit')")
    @Log(title = "库位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JcWarehouseinfoKuwei jcWarehouseinfoKuwei)
    {
        return toAjax(jcWarehouseinfoKuweiService.updateJcWarehouseinfoKuwei(jcWarehouseinfoKuwei));
    }

    /**
     * 删除库位管理
     */
    @PreAuthorize("@ss.hasPermi('base:warehouseinfoKuwei:remove')")
    @Log(title = "库位管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(jcWarehouseinfoKuweiService.deleteJcWarehouseinfoKuweiByIds(ids));
    }
}
